package com.dofast.bt.mvp.presenter.my;

import android.util.Log;

import com.dofast.bt.adapter.CollectGoodsListAdapter;
import com.dofast.bt.adapter.CollectStoreListAdapter;
import com.dofast.bt.adapter.SelectCallBack;
import com.dofast.bt.base.BaseMvpPresenter;
import com.dofast.bt.bean.EnjoyBean;
import com.dofast.bt.bean.GoodsBean;
import com.dofast.bt.mvp.model.IMainModel;
import com.dofast.bt.mvp.model.MainModel;
import com.dofast.bt.mvp.view.activity.my.CollectGoodsListView;
import com.dofast.bt.mvp.view.activity.my.CollectStoreListView;
import com.dofast.bt.util.Helper;

import org.xutils.ex.DbException;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * username:Created by hsj on 2016/12/22 0022.
 * class：
 * 类描述：收藏的宝贝
 */

public class CollectGoodsListPresenter extends BaseMvpPresenter<CollectGoodsListView> implements ICollectGoodsListPresenter {
    private List<EnjoyBean> list = null;
    private CollectGoodsListAdapter adapter = null;
    private GoodsBean goodsBean = null;
    private IMainModel mainModel;
    public CollectGoodsListPresenter(){
        mainModel = new MainModel();
    }

    @Override
    public void initData() {
        checkViewAttach();
        list = new ArrayList<EnjoyBean>();
        adapter = new CollectGoodsListAdapter(list, new SelectCallBack() {
            @Override
            public void select(int position, boolean flags) {
                // TODO: 2016/12/22 0022  删除逻辑处理
                try {
                    mainModel.cancelEnjoyGoods(list.get(position).getGoodsId());
                    getData();
                } catch (DbException e) {
                    e.printStackTrace();
                }
            }
        });
        goodsBean = new GoodsBean();
        mvpView.initAdapter(adapter);
        getData();
    }

    /**
     * 获取数据
     */
    private void getData() {
        try {
            if (mainModel.getEnjoy() != null){
                list.clear();
                list.addAll(mainModel.getEnjoy());
                adapter.notifyDataSetChanged();
            }
        } catch (DbException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void itemClick(int position) {
        try {
            goodsBean = list.get(position).getGoods();
            //跳转到详情界面
            mvpView.showDetail(goodsBean);
        } catch (DbException e) {
            e.printStackTrace();
        }



    }

    @Override
    public void delete(int position) {

        try {
            if ( mainModel.cancelEnjoyGoods(list.get(position).getGoodsId())){
                Helper.showToast("删除成功");
                getData();
            }else {
              //  Helper.showToast("操作失败");
            }


        } catch (DbException e) {
            e.printStackTrace();
        }

    }

    @Override
    public void showDelete(int position) {
        list.get(position).setCheck(true);
        adapter.notifyDataSetChanged();
    }
}
